package com.yc.vote.biz;

import com.yc.vote.mapper.TpRecordMapper;
import com.yc.vote.util.MBHelper;
import org.apache.ibatis.session.SqlSession;

public class TpRecordBiz {

    public void vote(String vid, String[] iids, String uid) throws BizException {
        if (iids == null || iids.length == 0) {
            throw new BizException("请选择项目!");
        }
        SqlSession session = MBHelper.openSession(false);
        try {
            TpRecordMapper mapper = session.getMapper(TpRecordMapper.class);
            if (mapper.count(vid, uid) > 0) {
                throw new BizException("您已经对该主题投过票了!");
            }
            for (String iid : iids) {
                mapper.insert(vid, iid, uid);
            }
            session.commit();
        } catch (BizException e) {
            session.rollback();
            throw e;
        } catch (Exception e) {
            session.rollback();
            throw new BizException("系统异常,请联系管理员!", e);
        } finally {
            session.close();
        }

    }
}
